home *** CD-ROM | disk | FTP | other *** search
/ Treccani Italiana Di Scienze Lettere Ed Arti / [Enciclopedia] Treccani Italiana di scienze lettere ed arti.iso / mac / data / menu_dvd.swf / scripts / __Packages / StringUtils.as < prev   
Text File  |  2007-11-07  |  13KB  |  434 lines

  1. class StringUtils
  2. {
  3.    static var BACKSLASH = String.fromCharCode(92);
  4.    static var APICE = String.fromCharCode(39);
  5.    static var DOPPIOAPICE = String.fromCharCode(34);
  6.    static var TABSPACE = "    ";
  7.    static var IMAGE_URL = "";
  8.    static var USE_SUB_SUP_FONT = false;
  9.    static var mImageCounter = 0;
  10.    function StringUtils()
  11.    {
  12.    }
  13.    static function addSlashes(string)
  14.    {
  15.       var outStr = "";
  16.       var i = 0;
  17.       while(i < string.length)
  18.       {
  19.          if(string.charAt(i) == StringUtils.BACKSLASH)
  20.          {
  21.             outStr += StringUtils.BACKSLASH + StringUtils.BACKSLASH;
  22.          }
  23.          else if(string.charAt(i) == StringUtils.APICE)
  24.          {
  25.             outStr += StringUtils.BACKSLASH + StringUtils.APICE;
  26.          }
  27.          else if(string.charAt(i) == StringUtils.DOPPIOAPICE)
  28.          {
  29.             outStr += StringUtils.BACKSLASH + StringUtils.DOPPIOAPICE;
  30.          }
  31.          else
  32.          {
  33.             outStr += string.charAt(i);
  34.          }
  35.          i++;
  36.       }
  37.       return outStr;
  38.    }
  39.    static function stripTrailingSpaces(string)
  40.    {
  41.       string = string;
  42.       string = StringUtils.replace(string,"\r","");
  43.       string = StringUtils.replace(string,"\n","");
  44.       string = StringUtils.replace(string,"\t","");
  45.       string = StringUtils.trim(string);
  46.       return string;
  47.    }
  48.    static function replace(s, arg_search, arg_replace)
  49.    {
  50.       StringUtils.string = s;
  51.       var arg_search;
  52.       var arg_replace;
  53.       var position;
  54.       var endText = "";
  55.       var preText = "";
  56.       var newText = "";
  57.       if(arg_search.length == 1)
  58.       {
  59.          return StringUtils.string.split(arg_search).join(arg_replace);
  60.       }
  61.       position = StringUtils.string.indexOf(arg_search);
  62.       if(position == -1)
  63.       {
  64.          return StringUtils.string;
  65.       }
  66.       endText = StringUtils.string;
  67.       do
  68.       {
  69.          position = endText.indexOf(arg_search);
  70.          preText = endText.substring(0,position);
  71.          endText = endText.substring(position + arg_search.length);
  72.          newText += preText + arg_replace;
  73.       }
  74.       while(endText.indexOf(arg_search) != -1);
  75.       
  76.       newText += endText;
  77.       return newText;
  78.    }
  79.    static function trim(s)
  80.    {
  81.       StringUtils.string = s;
  82.       var j;
  83.       var strlen;
  84.       var k;
  85.       strlen = StringUtils.string.length;
  86.       j = 0;
  87.       while(StringUtils.string.charAt(j) == " ")
  88.       {
  89.          j++;
  90.       }
  91.       if(j)
  92.       {
  93.          StringUtils.string = StringUtils.string.substr(j + 1,strlen);
  94.          if(j == strlen)
  95.          {
  96.             return StringUtils.string;
  97.          }
  98.       }
  99.       var k = StringUtils.string.length - 1;
  100.       while(StringUtils.string.charAt(k) == " ")
  101.       {
  102.          k--;
  103.       }
  104.       StringUtils.string = StringUtils.string.substr(1,k + 1);
  105.       return StringUtils.string;
  106.    }
  107.    static function trimWhiteAndCr(s)
  108.    {
  109.       StringUtils.string = s;
  110.       var j;
  111.       var strlen;
  112.       var k;
  113.       strlen = StringUtils.string.length;
  114.       j = 0;
  115.       while(StringUtils.string.charAt(j) == " " || StringUtils.string.charCodeAt(j) == 13 || StringUtils.string.charCodeAt(j) == 10)
  116.       {
  117.          j++;
  118.       }
  119.       if(j)
  120.       {
  121.          StringUtils.string = StringUtils.string.substr(j + 1,strlen);
  122.          if(j == strlen)
  123.          {
  124.             return StringUtils.string;
  125.          }
  126.       }
  127.       var k = StringUtils.string.length - 1;
  128.       while(StringUtils.string.charAt(k) == " " || StringUtils.string.charCodeAt(j) == 13 || StringUtils.string.charCodeAt(j) == 10)
  129.       {
  130.          k--;
  131.       }
  132.       StringUtils.string = StringUtils.string.substr(1,k + 1);
  133.       return StringUtils.string;
  134.    }
  135.    static function formatUrl(url)
  136.    {
  137.       if(url.indexOf("ftp://") != -1 || url.indexOf("news://") != -1 || url.indexOf("https://") != -1)
  138.       {
  139.          return url;
  140.       }
  141.       if(url.indexOf("http://") == -1 && url.indexOf("www") == -1)
  142.       {
  143.          return "http://www." + url;
  144.       }
  145.       if(url.indexOf("http://") != -1 && url.indexOf("www") == -1)
  146.       {
  147.          return StringUtils.replace(url,"http://","http://www.");
  148.       }
  149.       if(url.indexOf("http://") == -1)
  150.       {
  151.          return "http://" + url;
  152.       }
  153.    }
  154.    static function HtmlFunction(inLabel)
  155.    {
  156.       var outV = inLabel;
  157.       StringUtils.mImageCounter = 0;
  158.       var haveSup = outV.indexOf("<sup>") >= 0;
  159.       var haveSub = outV.indexOf("<sub>") >= 0;
  160.       var haveImg = outV.indexOf("<img ") >= 0;
  161.       if(haveSup || haveSub || haveImg)
  162.       {
  163.          outV = StringUtils.subSupImgFromHTML(outV,haveSup,haveSub,haveImg);
  164.          if(haveSup && outV.indexOf("#A|") >= 0)
  165.          {
  166.             outV = outV.split("#A|").join("<span class=\"s_Superscript\">");
  167.             outV = outV.split("|A#").join("</span>");
  168.          }
  169.          if(haveSub && outV.indexOf("#P|") >= 0)
  170.          {
  171.             outV = outV.split("#P|").join("<span class=\"s_Subscript\">");
  172.             outV = outV.split("|P#").join("</span>");
  173.          }
  174.       }
  175.       if(StringUtils.USE_SUB_SUP_FONT)
  176.       {
  177.          outV = outV.split("<b>").join("<span class=\"s_Bold\">");
  178.          outV = outV.split("</b>").join("</span>");
  179.       }
  180.       return outV;
  181.    }
  182.    static function subSupImgFromHTML(origHTML, haveSup, haveSub, haveImg)
  183.    {
  184.       var xmlString = new XML();
  185.       var outV;
  186.       xmlString.parseXML(origHTML);
  187.       if(haveSup || haveSub)
  188.       {
  189.          StringUtils.switchChars(xmlString);
  190.       }
  191.       if(StringUtils.IMAGE_URL.length > 0 && haveImg)
  192.       {
  193.          StringUtils.handleEmbeddedImage(xmlString);
  194.       }
  195.       outV = xmlString.toString();
  196.       return outV;
  197.    }
  198.    static function subSupFromHTML(origHTML)
  199.    {
  200.       var xmlString = new XML();
  201.       var outV = "";
  202.       xmlString.parseXML(origHTML);
  203.       StringUtils.switchChars(xmlString);
  204.       outV = xmlString.toString();
  205.       if(outV.indexOf("#A|") >= 0)
  206.       {
  207.          outV = outV.split("#A|").join("<span class=\"s_Superscript\">");
  208.          outV = outV.split("|A#").join("</span>");
  209.       }
  210.       if(outV.indexOf("#P|") >= 0)
  211.       {
  212.          outV = outV.split("#P|").join("<span class=\"s_Subscript\">");
  213.          outV = outV.split("|P#").join("</span>");
  214.       }
  215.       if(StringUtils.USE_SUB_SUP_FONT)
  216.       {
  217.          outV = outV.split("<b>").join("<span class=\"s_Bold\">");
  218.          outV = outV.split("</b>").join("</span>");
  219.       }
  220.       return outV;
  221.    }
  222.    static function handleEmbeddedImage(node)
  223.    {
  224.       if(node.nodeName != null)
  225.       {
  226.          if(node.nodeName == "img")
  227.          {
  228.             StringUtils.mImageCounter++;
  229.             var sourceData = node.attributes.src;
  230.             var sourceDataArray = sourceData.split("#");
  231.             node.attributes.src = StringUtils.IMAGE_URL + sourceDataArray[0];
  232.             node.attributes.width = parseInt(sourceDataArray[1]);
  233.             node.attributes.height = parseInt(sourceDataArray[2]);
  234.          }
  235.       }
  236.       var child = node.firstChild;
  237.       while(child != null)
  238.       {
  239.          StringUtils.handleEmbeddedImage(child);
  240.          child = child.nextSibling;
  241.       }
  242.    }
  243.    static function switchChars(node)
  244.    {
  245.       var curChar = "";
  246.       if(node.nodeName != null)
  247.       {
  248.          if(node.nodeName == "sup")
  249.          {
  250.             var nText = "";
  251.             if(StringUtils.USE_SUB_SUP_FONT)
  252.             {
  253.                nText = "#A|" + node.firstChild.nodeValue + "|A#";
  254.             }
  255.             else
  256.             {
  257.                var supStringLen = node.firstChild.nodeValue.length;
  258.                var supString = node.firstChild.nodeValue;
  259.                var i = 0;
  260.                while(i < supStringLen)
  261.                {
  262.                   curChar = supString.charAt(i);
  263.                   switch(curChar)
  264.                   {
  265.                      case "0":
  266.                         nText += "Γü░";
  267.                         break;
  268.                      case "1":
  269.                         nText += "┬╣";
  270.                         break;
  271.                      case "2":
  272.                         nText += "┬▓";
  273.                         break;
  274.                      case "3":
  275.                         nText += "┬│";
  276.                         break;
  277.                      case "4":
  278.                         nText += "Γü┤";
  279.                         break;
  280.                      case "5":
  281.                         nText += "Γü╡";
  282.                         break;
  283.                      case "6":
  284.                         nText += "Γü╢";
  285.                         break;
  286.                      case "7":
  287.                         nText += "Γü╖";
  288.                         break;
  289.                      case "8":
  290.                         nText += "Γü╕";
  291.                         break;
  292.                      case "9":
  293.                         nText += "Γü╣";
  294.                         break;
  295.                      case "+":
  296.                         nText += "Γü║";
  297.                         break;
  298.                      case "=":
  299.                         nText += "Γü╝";
  300.                         break;
  301.                      case "(":
  302.                         nText += "Γü╜";
  303.                         break;
  304.                      case ")":
  305.                         nText += "Γü╛";
  306.                         break;
  307.                      case "n":
  308.                         nText += "Γü┐";
  309.                         break;
  310.                      case "-":
  311.                      case String.fromCharCode(8722):
  312.                         nText += "Γü╗";
  313.                         break;
  314.                      default:
  315.                         nText += "#A|" + curChar + "|A#";
  316.                         break;
  317.                   }
  318.                   i++;
  319.                }
  320.             }
  321.             node.firstChild.nodeValue = nText;
  322.          }
  323.          else if(node.nodeName == "sub")
  324.          {
  325.             var nText = "";
  326.             if(StringUtils.USE_SUB_SUP_FONT)
  327.             {
  328.                nText = "#P|" + node.firstChild.nodeValue + "|P#";
  329.             }
  330.             else
  331.             {
  332.                var subStringLen = node.firstChild.nodeValue.length;
  333.                var subString = node.firstChild.nodeValue;
  334.                var i = 0;
  335.                while(i < subStringLen)
  336.                {
  337.                   curChar = subString.charAt(i);
  338.                   switch(curChar)
  339.                   {
  340.                      case "0":
  341.                         nText += "ΓéÇ";
  342.                         break;
  343.                      case "1":
  344.                         nText += "Γéü";
  345.                         break;
  346.                      case "2":
  347.                         nText += "Γéé";
  348.                         break;
  349.                      case "3":
  350.                         nText += "Γéâ";
  351.                         break;
  352.                      case "4":
  353.                         nText += "Γéä";
  354.                         break;
  355.                      case "5":
  356.                         nText += "Γéà";
  357.                         break;
  358.                      case "6":
  359.                         nText += "Γéå";
  360.                         break;
  361.                      case "7":
  362.                         nText += "Γéç";
  363.                         break;
  364.                      case "8":
  365.                         nText += "Γéê";
  366.                         break;
  367.                      case "9":
  368.                         nText += "Γéë";
  369.                         break;
  370.                      case "+":
  371.                         nText += "Γéè";
  372.                         break;
  373.                      case "-":
  374.                         nText += "Γéï";
  375.                         break;
  376.                      case "=":
  377.                         nText += "Γéî";
  378.                         break;
  379.                      case "(":
  380.                         nText += "Γéì";
  381.                         break;
  382.                      case ")":
  383.                         nText += "ΓéÄ";
  384.                         break;
  385.                      default:
  386.                         nText += "#P|" + curChar + "|P#";
  387.                   }
  388.                   i++;
  389.                }
  390.             }
  391.             node.firstChild.nodeValue = nText;
  392.          }
  393.       }
  394.       var child = node.firstChild;
  395.       while(child != null)
  396.       {
  397.          StringUtils.switchChars(child);
  398.          child = child.nextSibling;
  399.       }
  400.    }
  401.    static function TabToSpace(inTabNumber)
  402.    {
  403.       var outV = "";
  404.       var i = 0;
  405.       while(i < inTabNumber)
  406.       {
  407.          outV += StringUtils.TABSPACE;
  408.          i++;
  409.       }
  410.       return outV;
  411.    }
  412.    static function HtmlToText(inHtml)
  413.    {
  414.       if(!_global.gHtmlRenderer)
  415.       {
  416.          _global.gHtmlRenderer = _root.createTextField("txt_globalhtmlRenderer",1,0,0,100,200);
  417.          _global.gHtmlRenderer.html = true;
  418.          _global.gHtmlRenderer._visible = false;
  419.          _global.gHtmlRenderer.multiline = true;
  420.       }
  421.       _global.gHtmlRenderer.htmlText = inHtml;
  422.       return _global.gHtmlRenderer.text;
  423.    }
  424.    static function StripExt(inFilename)
  425.    {
  426.       var dotPos = inFilename.lastIndexOf(".");
  427.       if(dotPos >= 0)
  428.       {
  429.          return inFilename.substring(0,dotPos);
  430.       }
  431.       return inFilename;
  432.    }
  433. }
  434.